package com.chinese.converter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Dictionary {
	protected String name;
	protected String config;
	protected List<Map<String, String>> dictChain;
	protected Map<String, String> dict;

	public Dictionary(String config) {
		this.dictChain = new ArrayList<Map<String, String>>();
		this.name = "";
		this.config = "";
		setConfig(config);
	}

	public String getDictName() {
		return this.name;
	}

	public void setConfig(String config) {
		config = config.toLowerCase();
		if (this.config.equals(config)) {
			return;
		}
		this.dictChain.clear();
		if("hk2s".equals(config)||"s2hk".equals(config)
				||"s2t".equals(config)
				||"s2tw".equals(config)
				||"s2twp".equals(config)
				||"t2hk".equals(config)
				||"t2s".equals(config)
				||"t2tw".equals(config)
				||"tw2s".equals(config)
				||"tw2sp".equals(config)){
			this.config = config;
		}else{
			this.config="s2t";
		}
		loadDict();
	}

	private void loadDict() {
		this.dictChain.clear();
		List<String> dictFileNames = new ArrayList<String>();
		try {
			String filename = "config/" + this.config + ".json";
			InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			StringBuffer sb = new StringBuffer();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			JSONObject dictRoot = JSONObject.parseObject(sb.toString());
			this.name = dictRoot.getString("name");
			JSONArray jsonArray = dictRoot.getJSONArray("conversion_chain");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject dictObj = jsonArray.getJSONObject(i).getJSONObject("dict");
				dictFileNames.addAll(getDictFileNames(dictObj));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (String filename : dictFileNames) {
			this.dict = new HashMap<String, String>();
			filename = "dictionary/" + filename;
			try {
				InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
				String l = null;
				List<String> lines = new ArrayList<String>();
				while ((l = reader.readLine()) != null) {
					lines.add(l);
				}
				for (String line : lines) {
					String[] words = line.trim().split("\t");
					this.dict.put(words[0], words[1]);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			this.dictChain.add(this.dict);
		}
	}

	private List<String> getDictFileNames(JSONObject dictObject) {
		List<String> filenames = new ArrayList<String>();
		String type = (String) dictObject.get("type");
		if (type.equals("txt")) {
			filenames.add((String) dictObject.get("file"));
		} else if (type.equals("group")) {
			JSONArray dictGroup = (JSONArray) dictObject.get("dicts");
			for (Object obj : dictGroup) {
				filenames.addAll(getDictFileNames((JSONObject) obj));
			}
		}
		return filenames;
	}

	public List<Map<String, String>> getDictChain() {
		return this.dictChain;
	}
}
